import { houseOrderList, houseOrders } from '@/api/status'
import moment from 'moment'
const status = {
  state: {
    currentSelectedDate: moment(new Date()).format('YYYY-MM-DD'), // 时间轴当前选中日期
    ldsjSelectedDate: '', // 预定房源专用保存查询记录
    rooms: [], // 七天剩余房源
    orderList: [], // 订单列表
    orders: null // 根据日期查询订单列表
  },
  mutations: {
    setOrders (state, data) {
      state.orders = data || []
    },
    setOrderList (state, data) {
      state.orderList = data
    },
    setCurrentSelectedDate (state, data) {
      state.currentSelectedDate = data
      console.log('setCurrentSelectedDate', data)
    },
    setLdsjSelectedDate (state, data) {
      state.ldsjSelectedDate = data
    }
  },
  actions: {
    getHouseOrderList ({ commit }, data) {
      return new Promise((resolve, reject) => {
        try {
          houseOrderList(data).then((res) => {
            const data = res.data
            // 添加房号排序
            // Object.keys(data).forEach(key => {
            //   data[key].date.sort((first, last) => {
            //     const firstFh = Number(first.fh.replace(/[^0-9]/ig, ''))
            //     const lastFh = Number(last.fh.replace(/[^0-9]/ig, ''))
            //     return firstFh - lastFh
            //   })
            // })
            commit('setOrderList', data)
            resolve(data)
          }).catch(error => reject(error))
        } catch (error) {
          reject(error)
        }
      })
    },
    getHouseOrders ({ commit }, data) {
      return new Promise((resolve, reject) => {
        try {
          houseOrders(data).then((res) => {
            const data = res.data
            const setorders = data.date
            // 添加排序
            // setorders.sort((first, last) => {
            //   const firstFh = Number(first.fh.replace(/[^0-9]/ig, ''))
            //   const lastFh = Number(last.fh.replace(/[^0-9]/ig, ''))
            //   return firstFh - lastFh
            // })
            commit('setOrders', setorders)
            resolve(data)
          }).catch(error => reject(error))
        } catch (error) {
          reject(error)
        }
      })
    }
  }
}
export default status
